#include "common.h"
PUBLIC S4B StringCopy(PCHAR destination,PCHAR source,U4B length)
{
   U4B i;
   if(destination==(PCHAR)NULL || source==(PCHAR)NULL) return ARTIST_ERROR_INVALIDPARAMETER;
   for(i=0;(i<length)&&(source[i]!='\0');i++)
   {
      destination[i]=source[i];
   }
   if(i>=length) return ARTIST_ERROR_FAIL;
   destination[i]='\0';
   return ARTIST_ERROR_OK;
}

PUBLIC S4B GetCharacterNumber(PCHAR string,PU4B number)
{
    U4B i=0;
    for(;(*string)!='\0';string++,i++);
	*number=i;
    return ARTIST_ERROR_OK;
}
PUBLIC S4B LocateFirstCharacter(PCHAR string,CHAR character,PPCHAR first)
{
    for(;(*string)!=character && (*string)!='\0';string++);
	if((*string)!=character) return ARTIST_ERROR_FAIL;
    *first=string;
	return ARTIST_ERROR_OK;
}

PUBLIC S4B LocateNextCharacter(PCHAR string,CHAR character,PPCHAR next)
{
    string++;
    for(;(*string)!=character && (*string)!='\0';string++);
	if((*string)!=character) return ARTIST_ERROR_FAIL;
	*next=string;
    return ARTIST_ERROR_OK;
}

PUBLIC S4B CompareString(PCHAR s1,CHAR e1,PCHAR s2,CHAR e2)
{
   for(;((*s1)!=(e1)) && ((*s1)!=('\0')) && ((*s2)!=(e2)) && ((*s2)!=('\0')  && ((*s1)==(*s2)));s1++,s2++);
   if(((*s1)=='\0' || (*s1)==e1 ) && ((*s2)=='\0' || (*s2)==e2)) {  return ARTIST_ERROR_OK;}
   return ARTIST_ERROR_FAIL;
}
PUBLIC S4B CharacterRepeatNumber(PCHAR string,CHAR character,PU4B number)
{
    U4B i=0;
    for(;(*string)!='\0';string++)
    {
       if((*string)==character) i++;
    }    
	*number=i;
    return ARTIST_ERROR_OK;
}

PUBLIC S4B LocateCharacter(PCHAR string,U4B serial,CHAR character,PPCHAR p) /*serial :1,2,3,4...*/
{
   if(serial==0) return ARTIST_ERROR_INVALIDPARAMETER;
   U4B i=0;
   for(;(*string)!='\0' && i!=serial;string++)
   {
      if((*string)==character) i++;
   }
   if(i==serial)
   {string--; *p=string;return ARTIST_ERROR_OK;}
   return ARTIST_ERROR_FAIL;
}

PUBLIC S4B StringCopyWithEnd(PCHAR destination,U4B length,PCHAR source,CHAR character)
{
   U4B j=0;
   for(;(*source)!='\0' && (*source)!=character && j<length;source++,destination++,j++)
         *destination=*source;
   destination[j]='\0';
   return ARTIST_ERROR_OK;
}

